package com.wan.uitls

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.support.design.internal.BottomNavigationItemView
import android.support.design.internal.BottomNavigationMenuView
import android.support.design.widget.BottomNavigationView
import android.widget.Toast
import com.google.gson.Gson
import com.wan.entity.LoginEntity
import java.util.*

class CommonUtils {

    companion object {
        const val LOAD_FINISHED = 1  //加载完成
        const val LOAD_LOADING = 0  //加载中
        const val LOAD_CANCLE = -1  //取消
        const val LOAD_ERROR = -2  //异常

        const val Intent_URL = "url"  //传递的url地址
        const val Intent_Title = "title" //传递的标题

        const val Cookie_Get = "Set-Cookie" //收到的cookie
        const val Cookie_Send = "cookie" //保存的cookie

        private const val Login_State = "login_state" //登陆状态 false
        private const val Login_Message = "login_message"   //账号信息


        private const val Search_History = "history" //搜索历史
        private const val sp_name = "wan_android"   //缓存


        const val Search_Text = "k"  //要搜索的字段
        const val Action_Result_Fragment_Refresh = "com.android.fragment.result_refresh_data" //搜索结果界面刷新请求数据 广播action

        const val Search_Change_Text = "change"  //改变的文字
        const val Action_Search_Change_Text = "com.android.activity.search_change_text" //搜索界面改变搜索文字 广播action

        fun toastWord(context: Context, word: String) {
            Toast.makeText(context, word, Toast.LENGTH_SHORT).show()
        }

        /**
         * 设置底部导航栏的 模式
         */
        @SuppressLint("RestrictedApi")
        fun disableShiftMode(bottomNavigationView: BottomNavigationView) {
            var bottomNavigationMenuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
            var shiftingMode = bottomNavigationMenuView.javaClass.getDeclaredField("mShiftingMode")
            shiftingMode.isAccessible = true
            shiftingMode.setBoolean(bottomNavigationMenuView, false)
            shiftingMode.isAccessible = false

            for (i in 0 until bottomNavigationMenuView.childCount) {
                var itemView = bottomNavigationMenuView.getChildAt(i) as BottomNavigationItemView
                itemView.setShiftingMode(false)
                itemView.setChecked(itemView.itemData.isChecked)
            }
        }

        fun sendIntent(context: Context, cla: Class<*>, url: String, title: String) {

            var intent = Intent(context, cla)
            intent.putExtra(Intent_URL, url)
            intent.putExtra(Intent_Title, title)
            context.startActivity(intent)
        }

        /**
         * 保存搜索历史
         */
        fun saveHistory(context: Context, historyList: ArrayList<String>) {
            if (historyList.isEmpty()) {
                return
            }
            var gson = Gson()
            var saveJson = gson.toJson(historyList)
            var preferences = context.getSharedPreferences(CommonUtils.sp_name, Context.MODE_PRIVATE)
            var edit = preferences.edit()
            edit.putString(CommonUtils.Search_History, saveJson)
            edit.apply()
        }

        /**
         * 获取搜索历史
         * @return 列表List
         */
        fun getHistory(context: Context): ArrayList<String> {
            var arrayList = ArrayList<String>()
            var sp = context.getSharedPreferences(CommonUtils.sp_name, Context.MODE_PRIVATE)
            var dataJson = sp.getString(CommonUtils.Search_History, "")
            if (!dataJson.isBlank()) {
                var gson = Gson()
                var getDataList = gson.fromJson<ArrayList<String>>(dataJson, ArrayList<String>()::class.java)
                arrayList.addAll(getDataList)
            }
            return arrayList
        }

        /**
         * 获取登陆状态
         * @return true 已登录 false 未登陆
         */
        fun getLoginState(context: Context): Boolean {
            var preferences = context.getSharedPreferences(CommonUtils.sp_name, Context.MODE_PRIVATE)
            return preferences.getBoolean(Login_State, false)
        }

        /**
         * 保存登陆状态
         */
        fun saveLoginState(context: Context, state: Boolean) {
            var preferences = context.getSharedPreferences(CommonUtils.sp_name, Context.MODE_PRIVATE)
            preferences.edit()
                    .putBoolean(Login_State, state)
                    .apply()
        }

        /**
         * 保存登陆信息
         */
        fun saveLoginMessage(context: Context, loginEntity: LoginEntity) {
            var preferences = context.getSharedPreferences(CommonUtils.sp_name, Context.MODE_PRIVATE)
            preferences.edit()
                    .putString(Login_Message, loginEntity.toString())
                    .apply()
        }

        /**
         * 获取登陆信息
         * @return loginEntity 登陆信息
         */
        fun getLogginMessage(context: Context): LoginEntity? {

            var preferences = context.getSharedPreferences(CommonUtils.sp_name, Context.MODE_PRIVATE)
            if (preferences.contains(Login_Message)) {
                var message = preferences.getString(Login_Message, "")
                var gson = Gson()
                return gson.fromJson<LoginEntity>(message, LoginEntity::class.java)
            }
            return null
        }
    }

}